package com.example.project22_editingmediaplayer.action;

import java.io.FileDescriptor;
import java.util.ArrayList;

import com.example.project22_editingmediaplayer.objects.AlBumObject;
import com.example.project22_editingmediaplayer.objects.ArtisObject;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.objects.VideoObject;
import com.example.provider.TblSong;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Log;

public class AudioUltility {
	private Context mContext;

	public AudioUltility(Context mContext) {
		this.mContext = mContext;
	}

	public ArrayList<SongObject> getAllSong() {
		ArrayList<SongObject> songs = new ArrayList<SongObject>();
		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

		final String id = MediaStore.Audio.Media._ID;
		final String path = MediaStore.Audio.Media.DATA;
		final String name = MediaStore.Audio.Media.TITLE;
		final String artist = MediaStore.Audio.Media.ARTIST;

		final String[] columns = { id, path, name, artist };
		String sortby=name+" ASC";
		Cursor cursor = mContext.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
				selection, null, sortby);
		while (cursor.moveToNext()) {
			try {
				SongObject objectSong = new SongObject();
				objectSong.set_id(Integer.valueOf(cursor.getString(0)));
				objectSong.set_path(cursor.getString(1));
				objectSong.set_name(cursor.getString(2));
				objectSong.set_artist(cursor.getString(3));
				songs.add(objectSong);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		cursor.close();
		return songs;
	}

	public ArrayList<SongObject> getAllSongByAlbum(int idAlbum) {
		ArrayList<SongObject> songs = new ArrayList<SongObject>();
		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 AND "
				+ MediaStore.Audio.Media.ALBUM_ID + "="
				+ String.valueOf(idAlbum);

		final String id = MediaStore.Audio.Media._ID;
		final String path = MediaStore.Audio.Media.DATA;
		final String name = MediaStore.Audio.Media.TITLE;
		final String artist = MediaStore.Audio.Media.ARTIST;

		final String[] columns = { id, path, name, artist };
		String sortby=name+" ASC";
		Cursor cursor = mContext.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
				selection, null, sortby);
		while (cursor.moveToNext()) {
			try {
				SongObject objectSong = new SongObject();
				objectSong.set_id(Integer.valueOf(cursor.getString(0)));
				objectSong.set_path(cursor.getString(1));
				objectSong.set_name(cursor.getString(2));
				objectSong.set_artist(cursor.getString(3));
				songs.add(objectSong);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		cursor.close();
		return songs;
	}

	public ArrayList<SongObject> getAllMusicByArtis(int idArtis) {
		ArrayList<SongObject> songs = new ArrayList<SongObject>();
		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 AND "
				+ MediaStore.Audio.Media.ARTIST_ID + "="
				+ String.valueOf(idArtis);
		final String id = MediaStore.Audio.Media._ID;
		final String path = MediaStore.Audio.Media.DATA;
		final String name = MediaStore.Audio.Media.TITLE;
		final String artist = MediaStore.Audio.Media.ARTIST;
		final String[] columns = { id, path, name, artist };
		String sortby=name+" ASC";
		Cursor cursor = mContext.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
				selection, null, sortby);
		while (cursor.moveToNext()) {
			try {
				SongObject objectSong = new SongObject();
				objectSong.set_id(Integer.valueOf(cursor.getString(0)));
				objectSong.set_path(cursor.getString(1));
				objectSong.set_name(cursor.getString(2));
				objectSong.set_artist(cursor.getString(3));
				songs.add(objectSong);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		cursor.close();
		return songs;
	}

	// lay mot ban ghi
	public SongObject getOneSongById(int _id) {
		SongObject objectSong = new SongObject();

		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 AND "
				+ MediaStore.Audio.Media._ID + " = " + String.valueOf(_id);

		final String id = MediaStore.Audio.Media._ID;
		final String path = MediaStore.Audio.Media.DATA;
		final String name = MediaStore.Audio.Media.TITLE;
		final String artist = MediaStore.Audio.Media.ARTIST;

		final String[] columns = { id, path, name, artist };

		Cursor cursor = mContext.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns,
				selection, null, null);
		if (cursor != null)
			cursor.moveToFirst();
		try {
			objectSong.set_id(Integer.valueOf(cursor.getString(0)));
			objectSong.set_path(cursor.getString(1));
			objectSong.set_name(cursor.getString(2));
			objectSong.set_artist(cursor.getString(3));
		} catch (Exception e) {
			// TODO: handle exception
		}
		cursor.close();
		return objectSong;

	}
	public Bitmap getAlbumart(int album_id) {
		Bitmap bm = null;
		try {
			final Uri sArtworkUri = Uri
					.parse("content://media/external/audio/albumart");

			Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

			ParcelFileDescriptor pfd = mContext.getContentResolver()
					.openFileDescriptor(uri, "r");
			if (pfd != null) {
				FileDescriptor fd = pfd.getFileDescriptor();
				bm = BitmapFactory.decodeFileDescriptor(fd);
			}
		} catch (Exception e) {
		}
		return bm;
	}

	public ArrayList<AlBumObject> getAllAlbum() {
		ArrayList<AlBumObject> albums = new ArrayList<AlBumObject>();

		ContentResolver cr = mContext.getContentResolver();
		final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
		final String _id = MediaStore.Audio.Albums._ID;
		final String album_name = MediaStore.Audio.Albums.ALBUM;
		final String artist = MediaStore.Audio.Albums.ARTIST;
		final String number_song = MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS;
		String sortby=album_name+" ASC";
		final String[] columns = { _id, album_name, artist, number_song };

		Cursor cursor = cr.query(uri, columns, null, null, sortby);
		while (cursor.moveToNext()) {
			try {
				AlBumObject objectAlbum = new AlBumObject();
				objectAlbum.set_id(Integer.valueOf(cursor.getString(0)));
				objectAlbum.set_nameAlbum(cursor.getString(1));
				objectAlbum.set_artistAlbum(cursor.getString(2));
				objectAlbum
						.set_numberSong(Integer.valueOf(cursor.getString(3)));
				objectAlbum.set_bitmap(getAlbumart(objectAlbum.get_id()));
				albums.add(objectAlbum);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		cursor.close();
		return albums;
	}

	public ArrayList<ArtisObject> getAllArtis() {
		ArrayList<ArtisObject> artisObjects = new ArrayList<ArtisObject>();

		ContentResolver cr = mContext.getContentResolver();
		final Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;

		final String _id = MediaStore.Audio.Artists._ID;
		final String _artis = MediaStore.Audio.Artists.ARTIST;
		final String number_album = MediaStore.Audio.Artists.NUMBER_OF_ALBUMS;
		final String number_song = MediaStore.Audio.Artists.NUMBER_OF_TRACKS;
		String sortby=_artis+" ASC";
		final String[] columns = { _id, _artis, number_album, number_song };
		Cursor cursor = cr.query(uri, columns, null, null, sortby);
		while (cursor.moveToNext()) {
			try {
				ArtisObject temp = new ArtisObject();
				temp.set_id(Integer.valueOf(cursor.getString(0)));
				temp.set_artis(cursor.getString(1));
				temp.setNumberAlbum(Integer.valueOf(cursor.getString(2)));
				temp.setNumberTracks(Integer.valueOf(cursor.getString(3)));
				artisObjects.add(temp);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		cursor.close();
		return artisObjects;
	}

	public void deleteSongInMediaStore(int id) {
		mContext.getContentResolver().delete(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				MediaStore.Audio.Media._ID + "=" + id, null);
	}

	public ArrayList<VideoObject> getAllVideo() {
		ArrayList<VideoObject> allVideo = new ArrayList<VideoObject>();
		ContentResolver cr = mContext.getContentResolver();
		final Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
		String[] proj = { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.DATA,
				MediaStore.Video.Media.DISPLAY_NAME,
				MediaStore.Video.Media.SIZE, MediaStore.Video.Media.ARTIST };
		String sortby=MediaStore.Video.Media.DISPLAY_NAME+" ASC";
		Cursor cursor = cr.query(uri, proj, null, null, sortby);
		while (cursor.moveToNext()) {
			try {
				VideoObject temp = new VideoObject();
				temp.set_id(Integer.valueOf(cursor.getString(0)));
				temp.set_artist(cursor.getString(4));
				temp.set_name(cursor.getString(2));
				temp.set_path(cursor.getString(1));
				temp.set_size(Integer.valueOf(cursor.getString(3)));
				temp.set_bitmap(MediaStore.Video.Thumbnails.getThumbnail(cr,
						Integer.valueOf(cursor.getString(0)),
						MediaStore.Video.Thumbnails.MINI_KIND, null));
				allVideo.add(temp);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		cursor.close();
		return allVideo;

	}

}
